package com.sgb.kjwl.viewmodel.manage

import android.content.Context
import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import com.sgb.kjwl.api.APIManager
import com.sgb.kjwl.model.entity.LoginEntity
import com.sgb.kjwl.model.entity.manage.special.CompanyEntity
import com.sgb.kjwl.model.entity.manage.special.SpecialMemberEntity
import com.sgb.kjwl.model.entity.user.AddressEntity
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity
import com.sgb.kjwl.view.ui.activity.manage.special.SpecialMemberActivity.Companion.REQUEST_CODE_ACCEPTANCE
import com.swgk.core.base.BaseViewModel
import com.swgk.core.base.api.ICallBack
import com.swgk.core.base.model.entity.BaseEntity
import com.swgk.core.base.model.entity.H5ReEntity
import com.swgk.core.base.model.entity.ListResponse
import com.swgk.core.util.MToast
import com.swgk.core.util.PickSelectEntity

class CompanySpecialViewModel : BaseViewModel(){


    private val mTitles = arrayListOf("全部", "待受理", "已通过", "已拒绝")

    private val mFilterTitles = arrayListOf("待受理")

    var titles = MutableLiveData<List<PickSelectEntity<Any>>>()

    var mAllAddress = MutableLiveData<ArrayList<AddressEntity<List<AddressEntity<List<AddressEntity<Any>>>>>>>()

    //成员受理
    val mSpecialMemberList = MutableLiveData<List<SpecialMemberEntity>>()

    //邀请成员
    val mInviteMemberList = MutableLiveData<List<SpecialMemberEntity>>()

    //邀请成员 全平台企业搜索
    val mInviteMemberCompanyList = MutableLiveData<List<CompanyEntity>>()

    //im信息
    val mImMsg = MutableLiveData<LoginEntity>()


    private var specialMemberPage = 1
    private val limit = 10
    private val specialMemberMap = HashMap<String, Any>()

    private var inviteMemberPage = 1
    private val inviteMemberMap = HashMap<String, Any>()

    private var inviteMemberCompanyPage = 1
    private val inviteMemberCompanyMap = HashMap<String, Any>()

    fun getTitle(){
        var list = mTitles.mapIndexed{index, s ->
            PickSelectEntity<Any>().apply {
                no = "".plus(index - 1)
                projectscale = s
            }
        }
        titles.postValue(list)
    }

    fun getFilterTitle(){
        var list = mFilterTitles.mapIndexed{index, s ->
            PickSelectEntity<Any>().apply {
                no = "".plus(index)
                projectscale = s
            }
        }
        titles.postValue(list)
    }


    /**
     * 所有地址选项
     */
    fun getAddressModel(context: Context?) {
        APIManager.getInstance().goodsAPI.cityList.enqueue(object : ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity<Any>>>>>>>>(true, true){
            override fun onSuccess(data: BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity<Any>>>>>>>?) {
                if (data != null && data.data != null){
                    mAllAddress.postValue(data.data as ArrayList<AddressEntity<List<AddressEntity<List<AddressEntity<Any>>>>>>)
                }
            }

        })
    }


    /**
     * 成员受理 列表
     * status 状态--0待受理 1已同意 2已拒绝
     */
    fun getSpecialMemberList(status : String, isLoadMore : Boolean){
        specialMemberPage = if (isLoadMore) specialMemberPage + 1 else 1
        specialMemberMap["page"] = specialMemberPage
        specialMemberMap["limit"] = limit
        specialMemberMap["status"] = status
        APIManager.getInstance().websiteInfoAPI.getSpecialMemberList(specialMemberMap).enqueue(object : ICallBack<BaseEntity<ListResponse<List<SpecialMemberEntity>>>>(true, true){
            override fun onSuccess(data: BaseEntity<ListResponse<List<SpecialMemberEntity>>>?) {
                if (data != null && data.data != null && data.data.list != null) {
                    mSpecialMemberList.postValue(data.data.list)
                }
            }

        })
    }

    /**
     * 邀请成员 列表
     * status 状态--0待受理 1已同意 2已拒绝
     */
    fun getInviteMemberList(status : String, isLoadMore : Boolean){
        inviteMemberPage = if (isLoadMore) inviteMemberPage + 1 else 1
        inviteMemberMap["page"] = inviteMemberPage
        inviteMemberMap["limit"] = limit
        inviteMemberMap["status"] = status
        APIManager.getInstance().websiteInfoAPI.getInviteMemberList(inviteMemberMap).enqueue(object : ICallBack<BaseEntity<ListResponse<List<SpecialMemberEntity>>>>(true, true){
            override fun onSuccess(data: BaseEntity<ListResponse<List<SpecialMemberEntity>>>?) {
                if (data != null && data.data != null && data.data.list != null) {
                    mInviteMemberList.postValue(data.data.list)
                }
            }

        })
    }

    /**
     * 邀请成员 提交
     */
    fun inviteAddPost(map : HashMap<String, Any>){
        APIManager.getInstance().websiteInfoAPI.inviteMemberPost(map).enqueue(object : ICallBack<BaseEntity<*>>(true, true){
            override fun onSuccess(data: BaseEntity<*>?) {
                if (data != null){
                    message.postValue("邀请成功")
                }
            }
        })
    }

    /**
     * 邀请成员 再次邀请
     */
    fun inviteAgainPost(map : HashMap<String, Any>){
        APIManager.getInstance().websiteInfoAPI.inviteAgainMemberPost(map).enqueue(object : ICallBack<BaseEntity<*>>(true, true){
            override fun onSuccess(data: BaseEntity<*>?) {
                if (data != null){
                    message.postValue("邀请成功")
                }
            }
        })
    }

    /**
     * 全平台搜索企业
     */
    fun getAllCompany(companyName : String, provinceNo : String, cityNo : String, areaNo : String, isLoadMore: Boolean){
        inviteMemberCompanyPage = if (isLoadMore) inviteMemberCompanyPage + 1 else 1
        inviteMemberCompanyMap["page"] = inviteMemberCompanyPage
        inviteMemberCompanyMap["limit"] = limit
        inviteMemberCompanyMap["provinceNo"] = provinceNo
        inviteMemberCompanyMap["cityNo"] = cityNo
        inviteMemberCompanyMap["districtNo"] = areaNo
        inviteMemberCompanyMap["companyName"] = companyName
        APIManager.getInstance().websiteInfoAPI.inviteMemberAllList(inviteMemberCompanyMap).enqueue(object : ICallBack<BaseEntity<ListResponse<List<CompanyEntity>>>>(true, true){
            override fun onSuccess(data: BaseEntity<ListResponse<List<CompanyEntity>>>?) {
                if (data != null && data.data != null && data.data.list != null) {
                    mInviteMemberCompanyList.postValue(data.data.list)
                }
            }

        })
    }

    /**
     * 列表详情 -- 受理 受理详情 邀请详情
     * var isNeedRequestCode = false
     */

     fun getDetail(mContext: Context, id: String?, name: String) {
        var param = ""
        var isNeedRequestCode = false
        if ("受理" == name){
           isNeedRequestCode = true
            param = "userCenterMemberAcceptUpdate"
        } else if ("受理详情" == name){
            param = "userCenterMemberAcceptUpdate"
        } else if ("邀请详情" == name){
            param = "userCenterInviteAcceptDetail"
        }
        if (TextUtils.isEmpty(param)) MToast.showToast(mContext, "请检查接口路由")
        getH5Url(mContext, id!!, param, isNeedRequestCode)
    }

    /**
     * 获取h5跳转链接
     */
     private fun getH5Url(mContext: Context, no: String, param: String, isNeedRequestCode: Boolean) {
        APIManager.getInstance().pickAPI.getUrlSetting(param)
            .enqueue(object : ICallBack<BaseEntity<H5ReEntity>?>(true, true) {
                override fun onSuccess(data: BaseEntity<H5ReEntity>?) {
                    if (data != null  ){
                        if (data.data != null) {
                            var url = data.data.url
                            url = "$url?no=$no"
                            if (isNeedRequestCode){
                                WebViewX5HostActivity.start(mContext, url, REQUEST_CODE_ACCEPTANCE)
                            } else {
                                WebViewX5HostActivity.start(mContext, url)
                            }
                        } else {
                            MToast.showToast(mContext, data.msg)
                        }
                    }
                }
            })
    }

    /**
     * 获取im账号
     *
     */
    fun getImAccount(siteNo : String, siteType : String){
       APIManager.getInstance().websiteInfoAPI.getImAccount(siteNo, siteType).enqueue(object : ICallBack<BaseEntity<LoginEntity>>(true, true){
           override fun onSuccess(data: BaseEntity<LoginEntity>?) {
              if (data != null && data.data != null){
                  mImMsg.postValue(data.data)
              }
           }
       })
    }
}